package xyz.robinbingo.turing.dubbo.common.proxy;

import xyz.robinbingo.turing.dubbo.common.protocol.ProtocolService;
import xyz.robinbingo.turing.dubbo.common.protocol.ProtocolServiceFactory;
import xyz.robinbingo.turing.dubbo.common.protocol.invocation.Invocation;

import java.lang.reflect.Proxy;

/**
 * ProxyFactory
 *
 * @author Robin
 * @date 2021/1/29 下午4:56
 **/
public class ProxyFactory {

    @SuppressWarnings("unchecked")
    public static <T> T getProxy(Class<T> clazz) {
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, (proxy, method, args) -> {
            Invocation invocation = Invocation.builder()
                    .interfaceName(clazz.getName())
                    .methodName(method.getName())
                    .paramTypes(method.getParameterTypes())
                    .params(args)
                    .build();

//            return new CustomHttpClient().send("localhost", 8080, invocation);
            // ---------- 3.0 支持多协议切换 ---------
            ProtocolService protocolService = ProtocolServiceFactory.getService();
            return protocolService.send("localhost", 8080, invocation);
        });
    }
}
